package com.gexiaobao.pigeon.ui.fragment.setting

import android.os.Bundle
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.App
import com.gexiaobao.pigeon.app.base.BaseFragment
import com.gexiaobao.pigeon.app.ext.initClose
import com.gexiaobao.pigeon.app.ext.showMessage
import com.gexiaobao.pigeon.app.model.param.EditPassWordParams
import com.gexiaobao.pigeon.app.util.RxToast
import com.gexiaobao.pigeon.databinding.FragmentModifyPwdBinding
import com.gexiaobao.pigeon.ui.activity.LoginActivity
import com.gexiaobao.pigeon.viewmodel.PersonalInfoViewModel
import me.hgj.jetpackmvvm.ext.nav
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat
import me.hgj.jetpackmvvm.util.startActivity

/**
 * @Author      : hxw
 * @Date        : 2023/6/1 16:34
 * @Describe    : 修改密码
 */
class FragmentModifyPwd : BaseFragment<PersonalInfoViewModel, FragmentModifyPwdBinding>() {

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.viewmodel = mViewModel
        mDatabind.includeBar.toolbar.initClose("修改密码") {
            nav().navigateUp()
        }
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(mDatabind.updatePasswordCommit) {
            when (it) {
                mDatabind.updatePasswordCommit -> {
                    when {
                        mViewModel.oldPassWord.get().isEmpty() -> showMessage("请输入原密码")
                        mViewModel.newPassWord.get().isEmpty() -> showMessage("请输入新密码")
                        mViewModel.newPassWord2.get().isEmpty() -> showMessage("请再次输入新密码")
                        mViewModel.newPassWord.get() != mViewModel.newPassWord2.get() -> showMessage(
                            resources.getString(
                                R.string.user_password_not_alike
                            )
                        )

                        else -> {
                            val body = EditPassWordParams(oldPassWord = mViewModel.oldPassWord.get(), newPassWord = mViewModel.newPassWord.get())
                            mViewModel.editPwd(body)
                        }
                    }
                }
            }
        }
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.editPwdResult.observe(viewLifecycleOwner) {
            if (it.isSuccess) {
                startActivity<LoginActivity>()
                App.exitAll()
//                nav().navigateUp()
            } else {
                RxToast.showToast(it.errorMsg)
            }
        }
    }
}